<?php
header('Content-type: text/html; charset=utf-8');
$servername = "localhost";
$username   = "root";
$password   = "";
$dbname = "PHPlesson";

// 创建连接
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    mysql_query("SET NAMES 'utf8'");
    // 下面将点赞数据写入表中
    $num = $_REQUEST['num'];
    $creator = $username;
    $thumbUpTime = date('Y-m-d H:i:s');
    $stmt = $conn->prepare("SELECT `creator` FROM `thumbup` WHERE thumbup.creator = ?");
    $stmt->bind_param("s", $creator);

    if ($stmt->execute()) {
        $stmt = $conn->prepare("UPDATE `thumbup` SET `num`=?,`thumbUpTime`=? WHERE `creator`=?");
        $stmt->bind_param("sss", $num, $thumbUpTime, $creator);
    } else {
        $stmt = $conn->prepare("INSERT INTO `thumbup`(`creator`, `num`, `thumbUpTime`) VALUES (?, ?, ?)");
        $stmt->bind_param("sss", $creator, $num, $thumbUpTime);
    }
    
    $result = $stmt->execute();
    if ($result) {
        $result = array('resutlCode' => 0, 'resultMsg' => "点赞成功！");
    } else {
        $result = array('resutlCode' => 1, 'resultMsg' => "Error: " . $sql . "<br>" . mysql_error($conn));
    }
    echo json_encode($result);
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
?>
